#include "UITools.h"

UITools::UITools(QObject *parent)
    : QObject{parent}{
}

QString UITools::getJsonStrFromFile(QString pN, QString pI, QString fN){
    QString jsonPath = buildJsonPath(pN, pI, fN);

    if(!QFile(jsonPath).exists()){
        return "";
    }else{
        QFile file(jsonPath);
        if(file.open(QIODevice::ReadOnly | QIODevice::Text)){
            QTextStream stream(&file);
            QString res = stream.readAll();
            file.close();
            return res;
        }else{
            qCritical() << "read failed, path: " << jsonPath << Qt::endl;
            return "";
        }
    }
}

QString UITools::buildJsonPath(QString pN, QString pI, QString fN){
    QString fileName = Helper::_fixName(fN);
    size_t lastIndex = fileName.lastIndexOf('.');
    QString leftStr = fileName.left(lastIndex);
    const QString newFileName = leftStr + ".json";

    QString nPath = QDir(cachePath).filePath(pN);
    QString iPath = QDir(nPath).filePath(pI);
    QString jPath = QDir(iPath).filePath(newFileName);
    return jPath;
}


QString UITools::cahceReDownload(QString pN, QString pI, QString fN){
    // 根据此映射结构从远端重新下载json字符串
    return "";
}

